<?php
/**
 * Created by PhpStorm.
 * User: wangxiaodan
 * Date: 2017/5/8
 * Time: 下午1:35
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Http\Request;
use DB;


class RoleController extends Controller
{
    public function __construct()
    {
        //$this->middleware('role:admin');
    }

    public function index(){
        $roles = DB::table('roles')->paginate(PAGE_NUM);

        return view('admin.roles.index', compact('roles'));
    }

    public function create(){
        $perms = Permission::get();
        return view('admin.roles.create', compact('perms'));
    }

    public function store(Request $request){
      //汉字正则验证规则
      if(preg_match("/[\x7f-\xff]/", $request->jsname)){
        exit(json_encode(array('code'=>0,'msg'=>'角色名称不能为汉字')));
      }
        $this->validate($request, [
            'jsname' => 'required|max:20|min:3|unique:roles,name',
            'display_name'=>'required|max:10',
            'description'=>'max:20',
            'perm'=>'required|max:20'
        ]);
        $request->jsname = trim(str_replace(" ","",$request->jsname)," ");
        $request->display_name = trim(str_replace(" ","",$request->display_name)," ");
        $role = Role::create([
            'name'       => $request->jsname,
            'display_name' => $request->display_name,
            'description'  => $request->description,
        ]);
        if(!$role){
            exit(json_encode(array('code'=>0, 'msg'=>'角色名称不能为空')));
        }
        if($request->perm){
            $role->attachPermissions($request->perm);
            exit(json_encode(array('code'=>1, 'url'=>'/admin/roles')));
        }
    }

    public function edit($id){
        $role = Role::findOrFail($id);
        $perms = Permission::get();
        return view('admin.roles.edit', compact('role', 'perms'));
    }

    public function update(Request $request, $id){
      if(preg_match("/[\x7f-\xff]/", $request->jsname)){
        exit(json_encode(array('code'=>0,'msg'=>'角色名称不能为汉字')));
      }
        $role = Role::findOrFail($id);
        $this->validate($request, [
            'jsname' => 'required|max:20|min:2',
            'display_name'=>'required|max:10',
            'description'=>'max:20',
            'perm'=>'required',
        ]);
        $request->name = trim(str_replace(" ","",$request->name)," ");
        $request->display_name = trim(str_replace(" ","",$request->display_name)," ");
        $result = $role->fill([
            'name'         => $request->jsname,
            'display_name' => $request->display_name,
            'description'  => $request->description,
        ])->save();
        if(!$result){
            exit(json_encode(array('code'=>0, 'msg'=>$role)));
        }
        $role->savePermissions($request->perm);
        exit(json_encode(array('code'=>1, 'url'=>'/admin/roles')));
    }

    public function destroy($id){
        $role = Role::findOrFail($id);
        if ($role->id != 1){
          $z = DB::table('roles')->where('id','=',$role->id)->delete();
            if ($z){
                exit(json_encode(array('code'=>1)));
            } else {
                exit(json_encode(array('code'=>0)));
            }
        }

    }
}
